package com.luozongyao.admin.controller;


import com.luozongyao.admin.entity.Admin;
import com.luozongyao.admin.service.AdminService;
import com.luozongyao.commons.R;
import com.luozongyao.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luozongyao
 * @since 2021-11-05
 */
@Api(description="管理员信息管理")
@RestController
@RequestMapping("/admin/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "查询管理员信息")
    @GetMapping("getAdminInfo")
    public R getAdminInfo(){
        List<Admin> admins = adminService.list(null);
        System.out.println(admins);
        return R.ok().data("admin",admins);
    }

    @ApiOperation(value = "通过id查询管理员信息")
    @GetMapping("getAdminInfo/{id}")
    public R getAdminInfo(@PathVariable int id){
        Admin admin = adminService.getById(id);
        System.out.println(admin);
        return R.ok().data("admin",admin);
    }

    @PostMapping("updateAdmin")
    @ApiOperation(value = "更新修改管理员的方法")
    public R updateAdmin(@RequestBody Admin admin){
        admin.setAdminPwd(null);
        boolean flag = adminService.updateById(admin);
        System.out.println(admin);
        String token = JwtUtil.getToken(admin.getAdminId(), admin.getAdminName(), admin.getAdminAvatar());
        if (flag){
            return R.ok().data("token",token);
        }else {
            return R.error();
        }
    }

//    @PostMapping("updatePwdBehide")
//    @ApiOperation(value = "后门更新修改密码的方法")
//    public R updatePwdBehide(@RequestBody Admin admin){
//        String uuid = UUID.randomUUID().toString().replace("-","");
//        admin.setSalt(uuid);
//        admin.setAdminPwd(MD5.encrypt(MD5.encrypt(admin.getAdminPwd())+uuid));
//        boolean flag = adminService.updateById(admin);
//        System.out.println(admin);
//        if (flag){
//            return R.ok();
//        }else {
//            return R.error();
//        }
//    }
    @PostMapping("updatePwd")
    @ApiOperation(value = "更新修改密码的方法")
    public R updatePwd(@RequestBody Map<String,Object> info){
        //旧密码
        String oldPass = (String) info.get("oldPass");
        //新密码
        String pass = (String) info.get("pass");
        //当前登录用户ID
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updateAdminPassWord(oldPass,pass,adminId);
    }

}

